﻿#region <<版本注释>>
/*---------------------------------------------------------------------
* 项目名称：Http测试
* 项目描述：
* 类 名 称：MyHttpServer
* 类 描 述：
* 所在的域：DESKTOP-6CB1TV7
* 命名空间：Http测试
* 机器名称：DESKTOP-6CB1TV7
* CLR 版本：4.0.30319.42000
* 作    者：wky
* 创建时间：2021/09/02 15:49:40
* 更新时间：2021/09/02 15:49:40
* 版 本 号:V1.0.0
***********************************************************************
* Copyright @ wky 2021. All rights reserved.
***********************************************************************
//-------------------------------------------------------------------*/
#endregion

using Http测试.JsonTest;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Http测试
{
    class MyHttpServer:HttpServer
    {
        public MyHttpServer(int port)
            : base(port)
        {
        }

        public override void handleGETRequest(HttpProcessor p)
        {
            Console.WriteLine("GET request: {0}", p.http_url);
            p.writeSuccess();
            /*p.outputStream.WriteLine("<html><body><h1>test server</h1>");
            p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString());
            p.outputStream.WriteLine("url : {0}", p.http_url);

            p.outputStream.WriteLine("<form method=post action=/form>");
            p.outputStream.WriteLine("<input type=text name=foo value=foovalue>");
            p.outputStream.WriteLine("<input type=submit name=bar value=barvalue>");
            p.outputStream.WriteLine("</form>");*/

            
            /*int len = 100 * 1024;
            byte[] bytes = new byte[len];
            for (int i = 0; i < len; i++) {
                bytes[i] = (byte)(0x30 + (i % 2));
            }
            p.outputStream.WriteLine(Encoding.ASCII.GetString(bytes));*/

            AnimalBaseInfo abi = new AnimalBaseInfo(100, 200);
            string jsonStr = JsonHelper.ToJSON(abi);
            p.outputStream.WriteLine(jsonStr);
        }

        public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData)
        {
            Console.WriteLine("POST request: {0}", p.http_url);
            string data = inputData.ReadToEnd();
            p.writeSuccess();
            /*p.outputStream.WriteLine("<html><body><h1>test server</h1>");
            p.outputStream.WriteLine("<a href=/test>return</a><p>");
            p.outputStream.WriteLine("postbody: <pre>{0}</pre>", data);*/
            p.outputStream.WriteLine(data);
        }
    }
}
